package com.zhourh.bang.modular.edu.controller;

import com.zhourh.bang.common.annotion.Permission;
import com.zhourh.bang.common.annotion.log.BussinessLog;
import com.zhourh.bang.common.constant.Dict;
import com.zhourh.bang.common.controller.BaseController;
import com.zhourh.bang.common.exception.BizExceptionEnum;
import com.zhourh.bang.common.exception.BussinessException;
import com.zhourh.bang.common.node.ZTreeNode;
import com.zhourh.bang.common.persistence.dao.STermMapper;
import com.zhourh.bang.common.persistence.model.STerm;
import com.zhourh.bang.core.util.ToolUtil;
import com.zhourh.bang.modular.edu.dao.TermDao;
import com.zhourh.bang.modular.edu.service.ITermService;
import com.zhourh.bang.modular.system.warpper.MenuWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 学期控制器
 *
 * @author fengshuonan
 * @Date 2018-06-22 10:53:16
 */
@Controller
@RequestMapping("/term")
public class TermController extends BaseController {

    private String PREFIX = "/edu/term/";

    @Resource
    STermMapper termMapper;

    @Resource
    TermDao termDao;

    @Resource
    ITermService termService;

    /**
     * 跳转到学期首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "term.html";
    }

    /**
     * 跳转到添加学期
     */
    @RequestMapping("/term_add")
    public String termAdd() {
        return PREFIX + "term_add.html";
    }

    /**
     * 跳转到修改学期
     */
    @RequestMapping("/term_update/{termId}")
    public String termUpdate(@PathVariable Integer termId, Model model) {
        STerm term = this.termMapper.selectById(termId);
        model.addAttribute("term",term);
        return PREFIX + "term_edit.html";
    }

    /**
     * 获取年级的tree列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = this.termDao.tree();
        tree.add(ZTreeNode.createParent());
        return tree;
    }

    /**
     * 获取学期列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String key) {
        return this.termDao.list(key);
    }

    /**
     * 新增学期
     */
    @RequestMapping(value = "/add")
    @BussinessLog(value = "新增学期", key = "name", dict = Dict.TermMap)
    @Permission
    @ResponseBody
    public Object add(STerm term) {
        System.out.println(term);
        if (ToolUtil.isOneEmpty(term, term.getName(),term.getCode()  )) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        term.insert();
        return super.SUCCESS_TIP;
    }

    /**
     * 删除学期
     */
    @RequestMapping(value = "/delete")
    @BussinessLog(value = "删除学期", key = "name", dict = Dict.TermMap)
    @Permission
    @ResponseBody
    public Object delete(Integer termId) {
        return termMapper.deleteById(termId);
    }


    /**
     * 修改学期
     */
    @RequestMapping(value = "/update")
    @BussinessLog(value = "更新学期", key = "name", dict = Dict.TermMap)
    @Permission
    @ResponseBody
    public Object update(STerm term) {
        if(ToolUtil.isOneEmpty(term,term.getName(),term.getCode())){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        termMapper.updateById(term);
        return super.SUCCESS_TIP;
    }

    /**
     * 学期详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail(Integer termId) {
        return this.termMapper.selectById(termId);
    }
}
